public class Main {
    public static void main(String[] args) {
        System.out.println(new Solution().findMaxForm(new String[]{"10", "0001", "111001", "1", "0"}, 5, 3));
        System.out.println(new Solution().findMaxForm(new String[]{"10", "0", "1"}, 1, 1));
    }
}


class Solution {
    int[] count(String str) {
        int[] res = new int[2];
        for (int j = 0; j < str.length(); j++) {
            res[str.charAt(j) - '0']++;
        }
        return res;
    }

    public int findMaxForm(String[] strs, int m, int n) {
        int[][] dp = new int[m + 1][n + 1];
        for (String str : strs) {
            int[] count = count(str);
            int count0 = count[0];
            int count1 = count[1];
            for (int i = m; i >= count0; i--) {
                for (int j = n; j >= count1; j--) {
                    dp[i][j] = Math.max(dp[i - count0][j - count1] + 1, dp[i][j]);
                }
            }
        }
        return dp[m][n];
    }
}